import 'package:basic_structure_flutter/app/modules/home/home_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../mine/mine_view.dart';
import 'main_logic.dart';
import 'main_state.dart';

class MainPage extends StatefulWidget {
  const MainPage({Key? key}) : super(key: key);

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  final MainLogic logic = Get.put(MainLogic());
  final MainState state = Get.find<MainLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Container(child: GetBuilder<MainLogic>(
      builder: (_) {
        return Scaffold(
          body:SafeArea(
              child: IndexedStack(
                index: state.tabIndex,
                children: [
                  HomePage(),
                  MinePage(),
                ],
              )),
          backgroundColor: Colors.white,
            bottomNavigationBar: BottomNavigationBar(
                currentIndex: state.tabIndex,
                type: BottomNavigationBarType.fixed,
                selectedItemColor: Colors.blue,
                unselectedItemColor: Colors.grey,
                backgroundColor: Colors.white60,
                items: [
                  BottomNavigationBarItem(
                    icon: state.tabIndex == 0
                        ? Image.asset('assets/images/icon_home_selected.png')
                        : Image.asset('assets/images/icon_home_unselected.png'),
                    label: '首页',
                  ),
                  BottomNavigationBarItem(
                    icon: state.tabIndex == 1
                        ? Image.asset('assets/images/icon_mine_selected.png')
                        : Image.asset('assets/images/icon_mine_unselected.png'),
                    label: '我的',
                  ),
                ],
                onTap: (index) => {
                  logic.changeIndex(index)
                }
            ),
        );
      },
    ));
  }

  @override
  void dispose() {
    Get.delete<MainLogic>();
    super.dispose();
  }
}
